package net.samsb.activity;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import net.samsb.BaseApplication;
import net.samsb.R;
import net.samsb.log.LogUtils;
import net.samsb.model.DoorEntity;
import net.samsb.model.StudentMsgOrNumEntity;
import net.samsb.model.StudentNumEntity;
import net.samsb.model.WJsonEntity;
import net.samsb.model.WeatherEntity;
import net.samsb.tool.ActionBarTool;
import net.samsb.tool.MyDialog;
import net.samsb.tool.Tools;

import java.util.List;

import static net.samsb.tool.UrlTools.formatNum;
import static net.samsb.tool.UrlTools.formatURL;

public class MainActivity extends Constants {

    private TextView tv_time, tv_date, tv_week;

    //当天天气情况
    private ImageView img_weather;
    private TextView tv_weather_TAH, tv_weather_msg;

    //预报1
    private ImageView img_weather_one;
    private TextView tv_weather_city;
    private TextView tv_weather_time_one, tv_weather_one_msg, tv_weather_one_TAH_msg;

    //预报2
    private ImageView img_weather_two;
    private TextView tv_weather_time_two, tv_weather_two_msg, tv_weather_two_TAH_msg;

    //预报3
    private ImageView img_weather_three;
    private TextView tv_weather_time_three, tv_weather_three_msg, tv_weather_three_TAH_msg;

    //学校人数
    private TextView tv_on_school_sd_num, tv_late_sd_num, tv_leave_sd_num, tv_leave_not_return_sd_num;

    //学生信息
    private ImageView img_sd_avatar;
    private TextView tv_sd_name, tv_sd_class, tv_sd_code_value;

    private String domain;
    private String port;
    private String date;
    private String week;
    private String theme;

    private SharedPreferences mSharedPreferences = BaseApplication.mSharedPreferences;
    private SharedPreferences.Editor mEditor = BaseApplication.mEditor;

    private View view;
    private AlertDialog mBuilder;
    private AlertDialog doorListBuilder;
    private ProgressDialog waitingDialog;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case UPDATE_MAIN_THEME: {

                    BaseApplication.mHandler.clran();
                    mHandler.removeCallbacksAndMessages("MainActivity");
                    final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                    break;
                }

                case UPDATE_TIME_UI: {

                    String time = Tools.getSystemTimeByHMS("HH:mm:ss");

                    if (time != null && !"".equals(time)) {

                        if ("00:00:00".equals(time)) {
                            setDay();
                        }

                        tv_time.setText(time);
                    }
                    break;
                }

                case UPDATE_WS_CONNECT: {

                    //keep reconnect
                    if (msg.obj != null && "RECONNECT".equals(msg.obj.toString())) {

                        if (BaseApplication.BaseWebSocket != null) {

                            LogUtils.e("getWebSocketMessage", "RECONNECT_重连");

                            BaseApplication.BaseWebSocket.close(4444, null);

                            BaseApplication.BaseRequestInstance.getWebSocketRequest().getWebSocketMessage(null);
                        }
                    }

                    //error connect
                    if (msg.obj != null && "ERROR_RECONNECT".equals(msg.obj.toString())) {

                        LogUtils.e("getWebSocketMessage", "ERROR_RECONNECT_重连");

                        BaseApplication.BaseRequestInstance.getWebSocketRequest().getWebSocketMessage(null);

                    }

                    break;
                }

                case UPDATE_WEATHER_UI: {
                    if (msg.obj != null) {

                        if (mBuilder != null) {
                            mBuilder.dismiss();
                        }

                        if (msg.arg2 == -1) {

                            MyDialog.dismissDialog(doorListBuilder);

                            URL_DOMAIN_NAME = domain;
                            URL_PORT = port;

                            mEditor.putString("domain", domain);
                            mEditor.putString("port", port);
                            mEditor.commit();

                            BaseApplication.isFirstOpen = false;

                            LogUtils.e("save", "portSuccess");
                            LogUtils.e("save", "domain Success");

                            //默认头像
                            BaseApplication.BaseRequestInstance
                                    .getPictureRequest()
                                    .getImg(
                                            formatURL(URL_DOMAIN_NAME, DEFAULT_AVATAR),
                                            HOME_GET_AVATAR_IMG_UI,
                                            UI_UPDATE_AVATAR
                                    );

                        }

                        setWeatherDate((WJsonEntity) msg.obj);
                    }
                    break;
                }

                case HOME_GET_WEATHER_IMG_UI: {

                    if (msg.obj != null) {

                        if (msg.arg1 == UI_UPDATE_WEATHER_NOW) {
                            img_weather.setImageBitmap((Bitmap) msg.obj);
                        } else if (msg.arg1 == UI_UPDATE_WEATHER_ONE) {
                            img_weather_one.setImageBitmap((Bitmap) msg.obj);
                        } else if (msg.arg1 == UI_UPDATE_WEATHER_TWO) {
                            img_weather_two.setImageBitmap((Bitmap) msg.obj);
                        } else if (msg.arg1 == UI_UPDATE_WEATHER_THREE) {
                            img_weather_three.setImageBitmap((Bitmap) msg.obj);
                        } else if (msg.arg1 == UI_UPDATE_AVATAR) {
                            img_sd_avatar.setImageBitmap((Bitmap) msg.obj);
                        } else {
                            // Toast.makeText(MainActivity.this, "图片加载失败,请检查网络是否连接", Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        // Toast.makeText(MainActivity.this, "图片加载失败,请检查网络是否连接", Toast.LENGTH_SHORT).show();
                    }

                    break;
                }

                case HOME_GET_AVATAR_IMG_UI: {

                    if (msg.obj != null) {
                        img_sd_avatar.setImageBitmap((Bitmap) msg.obj);
                    }

                    break;
                }

                case UPDATE_STUDENT_MSG_OR_NUM_UI: {

                    if (msg.obj != null) {
                        setStudentNumDate(msg.obj, msg.arg1);
                    }

                    break;
                }

                case HOME_GET_DOOR_LIST: {

                    if (msg.obj != null) {
                        waitingDialog.dismiss();

                        if (msg.arg1 == 404) {
                            MyDialog.showDialog(MainActivity.this, "服务器IP不可用,请重新输入");
                            return;
                        }

                        MyDialog.showDialog(
                                MainActivity.this,
                                domain,
                                port,
                                (List<DoorEntity>) msg.obj,
                                waitingDialog,
                                doorListBuilder
                        );
                    }

                    break;
                }

                case ERROR: {

                    waitingDialog.dismiss();

                    if (doorListBuilder != null) {
                        MyDialog.dismissDialog(doorListBuilder);
                    }

                    if (msg.obj != null) {

                        if ("请求错误，请检查网络是否连接".equals(msg.obj)) {
                            // Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_LONG).show();
                            return;
                        }
                        MyDialog.showDialog(MainActivity.this, msg.obj.toString());
                        return;
                    }
                    break;
                }

                default:
                    MyDialog.showDialog(MainActivity.this, "未知错误,请检查网络");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        ActionBarTool.setActionBarTransparent(MainActivity.this);

        //init theme
        theme = mSharedPreferences.getString("themeType", "dark");

        if ("dark".equals(theme)) {

            setTheme(R.style.myTheme_Dark);
        }

        if ("day".equals(theme)) {

            setTheme(R.style.myTheme_Highlight);
        }

        setContentView(R.layout.activity_main);

        view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);

        BaseApplication.mHandler.setHandlerWithName("MainActivity", mHandler);

        waitingDialog = new ProgressDialog(MainActivity.this);
        waitingDialog.setTitle("提示");
        waitingDialog.setMessage("数据加载中...");
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(false);

        init();

        //更新时间
        new updateTime().start();

        //获取日期
        setDay();

        Log.e("MainActivity", "----- onCreate_theme");
    }

    private void init() {

        //引导
        settingGuide();

        img_weather = (ImageView) findViewById(R.id.img_weather);
        img_weather_one = (ImageView) findViewById(R.id.img_weather_one);
        img_weather_two = (ImageView) findViewById(R.id.img_weather_two);
        img_weather_three = (ImageView) findViewById(R.id.img_weather_three);

        //时间
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_date = (TextView) findViewById(R.id.tv_date);
        tv_week = (TextView) findViewById(R.id.tv_week);

        //当天天气情况
        tv_weather_city = (TextView) findViewById(R.id.tv_weather_city); //城市
        tv_weather_TAH = (TextView) findViewById(R.id.tv_weather_TAH); //温度情况
        tv_weather_msg = (TextView) findViewById(R.id.tv_weather_msg); //天气情况

        //预报1
        tv_weather_one_TAH_msg = (TextView) findViewById(R.id.tv_weather_one_TAH_msg);
        tv_weather_time_one = (TextView) findViewById(R.id.tv_weather_time_one); //时间
        tv_weather_one_msg = (TextView) findViewById(R.id.tv_weather_one_msg);

        //预报2
        tv_weather_two_TAH_msg = (TextView) findViewById(R.id.tv_weather_two_TAH_msg);
        tv_weather_time_two = (TextView) findViewById(R.id.tv_weather_time_two);
        tv_weather_two_msg = (TextView) findViewById(R.id.tv_weather_two_msg);

        //预报3
        tv_weather_three_TAH_msg = (TextView) findViewById(R.id.tv_weather_three_TAH_msg);
        tv_weather_time_three = (TextView) findViewById(R.id.tv_weather_time_three);
        tv_weather_three_msg = (TextView) findViewById(R.id.tv_weather_three_msg);

        //学校人数信息
        tv_on_school_sd_num = (TextView) findViewById(R.id.tv_on_school_sd_num);
        tv_late_sd_num = (TextView) findViewById(R.id.tv_late_sd_num);
        tv_leave_sd_num = (TextView) findViewById(R.id.tv_leave_sd_num);
        tv_leave_not_return_sd_num = (TextView) findViewById(R.id.tv_leave_not_return_sd_num);

        //学生信息
        img_sd_avatar = (ImageView) findViewById(R.id.img_sd_avatar);
        tv_sd_name = (TextView) findViewById(R.id.tv_sd_name);
        tv_sd_class = (TextView) findViewById(R.id.tv_sd_class);
        tv_sd_code_value = (TextView) findViewById(R.id.tv_sd_code_value);
    }

    private void setDay() {

        date = Tools.getSystemTimeByHMS("yyyy年MM月dd日");
        week = Tools.getWeek();

        if (date != null && !"".equals(date)) {
            tv_date.setText(date + "     ");
        }

        if (week != null && !"".equals(week)) {
            tv_week.setText(week);
        }
    }

    private void setWeatherDate(WJsonEntity wJsonEntity) {

        String time[];
        String picUrl;

        List<WeatherEntity> weatherEntities = wJsonEntity.getResult();

        WeatherEntity weatherEntityNow = weatherEntities.get(0);
        WeatherEntity weatherEntityLastOne = weatherEntities.get(1);
        WeatherEntity weatherEntityLastTwo = weatherEntities.get(2);
        WeatherEntity weatherEntityLastThree = weatherEntities.get(3);

        if (weatherEntityNow != null) {

            tv_weather_city.setText(weatherEntityNow.getCitynm());
            tv_weather_msg.setText(weatherEntityNow.getWeather());
            tv_weather_TAH.setText(weatherEntityNow.getTemp_high() + "℃-" + weatherEntityNow.getTemp_low() + "℃");

            picUrl = weatherEntityNow.getWeather_icon();

            if (picUrl != null && !"".equals(picUrl)) {
                getPictureRequest(
                        formatURL(URL_DOMAIN_NAME,
                                "images/d1",
                                Tools.getFileName(picUrl),
                                "png"), HOME_GET_WEATHER_IMG_UI, UI_UPDATE_WEATHER_NOW);
            }
        }

        if (weatherEntityLastOne != null) {

            picUrl = weatherEntityLastOne.getWeather_icon();
            time = weatherEntityLastOne.getDays().split("-");

            tv_weather_one_TAH_msg.setText(
                    weatherEntityLastOne.getTemp_high() +
                            "℃-" +
                            weatherEntityLastOne.getTemp_low() +
                            "℃"
            );

            tv_weather_time_one.setText(time[1] + "-" + time[2]);
            tv_weather_one_msg.setText(weatherEntityLastOne.getWeather());

            if (picUrl != null && !"".equals(picUrl)) {
                getPictureRequest(
                        formatURL(URL_DOMAIN_NAME,
                                "images/d1",
                                Tools.getFileName(picUrl),
                                "png"), HOME_GET_WEATHER_IMG_UI, UI_UPDATE_WEATHER_ONE);
            }
        }

        if (weatherEntityLastTwo != null) {

            picUrl = weatherEntityLastTwo.getWeather_icon();
            time = weatherEntityLastTwo.getDays().split("-");

            tv_weather_two_TAH_msg.setText(
                    weatherEntityLastTwo.getTemp_high() +
                            "℃-" +
                            weatherEntityLastTwo.getTemp_low() +
                            "℃"
            );
            tv_weather_time_two.setText(time[1] + "-" + time[2]);
            tv_weather_two_msg.setText(weatherEntityLastTwo.getWeather());

            if (picUrl != null && !"".equals(picUrl)) {
                getPictureRequest(
                        formatURL(URL_DOMAIN_NAME,
                                "images/d1",
                                Tools.getFileName(picUrl),
                                "png"), HOME_GET_WEATHER_IMG_UI, UI_UPDATE_WEATHER_TWO);
            }
        }

        if (weatherEntityLastThree != null) {

            picUrl = weatherEntityLastThree.getWeather_icon();
            time = weatherEntityLastThree.getDays().split("-");

            tv_weather_three_TAH_msg.setText(
                    weatherEntityLastThree.getTemp_high() +
                            "℃-" +
                            weatherEntityLastThree.getTemp_low() +
                            "℃"
            );

            tv_weather_time_three.setText(time[1] + "-" + time[2]);
            tv_weather_three_msg.setText(weatherEntityLastThree.getWeather());

            if (picUrl != null && !"".equals(picUrl)) {
                getPictureRequest(
                        formatURL(URL_DOMAIN_NAME,
                                "images/d1",
                                Tools.getFileName(picUrl),
                                "png"), HOME_GET_WEATHER_IMG_UI, UI_UPDATE_WEATHER_THREE);
            }
        }

    }

    private void setStudentNumDate(Object student, int type) {

        if (student != null) {

            String onSchool = null;
            String late = null;
            String leave = null;
            String leave_no = null;

            if (type == UPDATE_STUDENT_NOT_NAME_UI) {

                StudentNumEntity studentNumDate = (StudentNumEntity) student;

                if (studentNumDate.getZxrs() != null && !"".equals(studentNumDate.getZxrs())) {
                    onSchool = formatNum(studentNumDate.getZxrs());
                }

                if (studentNumDate.getCdrs() != null && !"".equals(studentNumDate.getCdrs())) {
                    late = formatNum(studentNumDate.getCdrs());
                }

                if (studentNumDate.getQjrs() != null && !"".equals(studentNumDate.getQjrs())) {
                    leave = formatNum(studentNumDate.getQjrs());
                }

                if (studentNumDate.getQjwg() != null && !"".equals(studentNumDate.getQjwg())) {
                    leave_no = formatNum(studentNumDate.getQjwg());
                }
            }

            if (type == UPDATE_STUDENT_MSG_UI) {

                StudentMsgOrNumEntity studentMsgOrNumEntity = (StudentMsgOrNumEntity) student;

                if (studentMsgOrNumEntity.getZxrs() != null && !"".equals(studentMsgOrNumEntity.getZxrs())) {
                    onSchool = formatNum(studentMsgOrNumEntity.getZxrs());
                }

                if (studentMsgOrNumEntity.getCdrs() != null && !"".equals(studentMsgOrNumEntity.getCdrs())) {
                    late = formatNum(studentMsgOrNumEntity.getCdrs());
                }

                if (studentMsgOrNumEntity.getQjrs() != null && !"".equals(studentMsgOrNumEntity.getQjrs())) {
                    leave = formatNum(studentMsgOrNumEntity.getQjrs());
                }

                if (studentMsgOrNumEntity.getQjwg() != null && !"".equals(studentMsgOrNumEntity.getQjwg())) {
                    leave_no = formatNum(studentMsgOrNumEntity.getQjwg());
                }

                if (studentMsgOrNumEntity.getXm() != null && !"".equals(studentMsgOrNumEntity.getXm())) {
                    tv_sd_name.setText("姓 名：" + studentMsgOrNumEntity.getXm());
                }

                if (studentMsgOrNumEntity.getBm() != null && !"".equals(studentMsgOrNumEntity.getBm())) {
                    tv_sd_class.setText("班 级：" + studentMsgOrNumEntity.getBm());
                }

                if (studentMsgOrNumEntity.getXgh() != null && !"".equals(studentMsgOrNumEntity.getXgh())) {
                    tv_sd_code_value.setText(studentMsgOrNumEntity.getXgh());
                }

                if (studentMsgOrNumEntity.getTxdz() != null && !"".equals(studentMsgOrNumEntity.getTxdz())) {
                    LogUtils.e("头像请求URL：", "HTTP://" + URL_DOMAIN_NAME + "/" + studentMsgOrNumEntity.getTxdz());
                    getPictureRequest(
                            formatURL(URL_DOMAIN_NAME, studentMsgOrNumEntity.getTxdz()),
                            HOME_GET_AVATAR_IMG_UI,
                            UI_UPDATE_AVATAR
                    );
                } else {
                    getPictureRequest(
                            formatURL(URL_DOMAIN_NAME, DEFAULT_AVATAR),
                            HOME_GET_AVATAR_IMG_UI,
                            UI_UPDATE_AVATAR
                    );
                }

            }

            tv_on_school_sd_num.setText(onSchool);
            tv_late_sd_num.setText(late);
            tv_leave_sd_num.setText(leave);
            tv_leave_not_return_sd_num.setText(leave_no);
        }
    }

    private void getPictureRequest(String picURL, int type, int imgCode) {

        BaseApplication.BaseRequestInstance
                .getPictureRequest()
                .getImg(picURL, type, imgCode);
    }

    /***
     * 首次setting引导
     */
    private void settingGuide() {

        String domain = mSharedPreferences.getString("domain", null);
        String port = mSharedPreferences.getString("port", null);
        String cookies = mSharedPreferences.getString("cookies", null);

        if (domain == null || cookies == null || "".equals(domain) || "".equals(cookies)) {
            Log.e("初始化", "空");
            showDialog();
            return;
        }

        URL_DOMAIN_NAME = domain;
        URL_PORT = port;
        URL_COOKIES = cookies;

        //默认头像
        getPictureRequest(
                formatURL(URL_DOMAIN_NAME, DEFAULT_AVATAR),
                HOME_GET_AVATAR_IMG_UI,
                UI_UPDATE_AVATAR
        );

        //获取数据
        waitingDialog.show();
        BaseApplication.BaseRequestInstance
                .getWebSocketRequest()
                .getWebSocketMessage(waitingDialog);
    }

    /***
     * Setting
     */
    private void showDialog() {

        View view = LayoutInflater.from(this).inflate(R.layout.activity_setting, null);

        final EditText ed_domain = (EditText) view.findViewById(R.id.editText_domain);
        final EditText ed_port = (EditText) view.findViewById(R.id.editText_port);
        final ImageView img_theme_type = (ImageView) view.findViewById(R.id.img_theme_type);

        Button btn_reset = (Button) view.findViewById(R.id.btn_reset);
        Button btn_submit = (Button) view.findViewById(R.id.btn_submit);

        TextView btn_sw_theme = (TextView) view.findViewById(R.id.btn_sw_theme);

        if (URL_DOMAIN_NAME != null && !"".equals(URL_DOMAIN_NAME)) {
            ed_domain.setText(URL_DOMAIN_NAME);
            Log.e("URL_DOMAIN_NAME", URL_DOMAIN_NAME);
        }

        if (URL_PORT != null && !"".equals(URL_PORT)) {
            ed_port.setText(URL_PORT);
            Log.e("URL_DOMAIN_PORT", URL_PORT);
        }

        mBuilder = new AlertDialog.Builder(this, R.style.muDialog).create();
        mBuilder.show();

        WindowManager.LayoutParams lp = mBuilder.getWindow().getAttributes();
        lp.width = ActionBar.LayoutParams.MATCH_PARENT;
        lp.height = ActionBar.LayoutParams.WRAP_CONTENT;
        mBuilder.getWindow().setAttributes(lp);
        mBuilder.getWindow().setContentView(view);
        mBuilder.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                domain = ed_domain.getText().toString();
                port = ed_port.getText().toString();

                if (domain == null || "".equals(domain)) {
                    MyDialog.showDialog(MainActivity.this, "请输入服务器IP");
                    return;
                }

                if (domain.indexOf(":") > 1) {

                    port = domain.split(":")[1];

                } else {
                    if (port == null || "".equals(port)) {
                        MyDialog.showDialog(MainActivity.this, "请输入服务器端口号");
                        return;
                    }
                }

                if ((URL_DOMAIN_NAME != null && URL_DOMAIN_NAME.equals(domain)) && (URL_PORT != null && URL_PORT.equals(port))) {
                    mBuilder.dismiss();
                    return;
                }

                mEditor = mSharedPreferences.edit();

                //Test
                if (BaseApplication.isFirstOpen //第一次打开
                        || URL_PORT == null || "".equals(URL_PORT) //ip正确
                        || (URL_DOMAIN_NAME != null && !URL_DOMAIN_NAME.equals(domain)) //修改ip
                        || (URL_PORT != null && !URL_PORT.equals(port))) { //修改端口

                    doorListBuilder = new AlertDialog.Builder(MainActivity.this).create();

                    waitingDialog.show();
                    BaseApplication
                            .BaseRequestInstance
                            .getDoorListRequest()
                            .geDoorListMessageTest(domain);

//                    if (domain.indexOf(":") > 1) {
//                        domain = domain.split(":")[0];
//                    }

                    return;
                }

//                waitingDialog.show();
//                BaseApplication.BaseRequestInstance
//                        .getWebSocketRequest()
//                        .getWebSocketMessage(port, waitingDialog);
            }
        });

        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ed_domain.setText("");
                ed_port.setText("");
            }
        });

        //切换主题
        btn_sw_theme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if ("dark".equals(theme)) {//切换为高亮主题

                    mEditor.putString("themeType", "day");

                    img_theme_type.setImageResource(R.drawable.icon_day);

                    //recreateOnResume();

                } else if ("day".equals(theme)) {//切换为暗主题

                    mEditor.putString("themeType", "dark");

                    img_theme_type.setImageResource(R.drawable.icon_dark);

                    //recreateOnResume();
                }

//                if (BaseApplication.BaseReconnectionWSThread != null ) {
//                    BaseApplication.BaseReconnectionWSThread.interrupt();
//                    BaseApplication.BaseReconnectionWSThread.isStop = true;
//                }

                mEditor.commit();

                mBuilder.dismiss();

//                BaseApplication.isReconnect = true; //启动重连线程

                Message msg = BaseApplication.mHandler
                       .getHandlerWithName("MainActivity")
                       .obtainMessage();
                msg.what = UPDATE_MAIN_THEME;
                mHandler.sendMessage(msg);

                //restartApp();
            }
        });
    }

    public void OnSetting(View view) {
        showDialog();
    }

    private class updateTime extends Thread {

        @Override
        public void run() {
            super.run();
            do {
                try {
                    Thread.sleep(1000);

                    Message msg = new Message();
                    msg.what = UPDATE_TIME_UI;
                    mHandler.sendMessage(msg);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.e("activity----", "onDestroy");

        if (waitingDialog != null) {
            waitingDialog.dismiss();
        }

//        if (BaseApplication.bootBroadcastReceiver != null) {
//            unregisterReceiver(BaseApplication.bootBroadcastReceiver);
//        }
    }
}
